<?php

class spGuiGroupedCheckboxes extends spDiv
{
	private $checkboxes = array();

	public function __construct( $param )
	{
		parent::__construct();
		parent::setClass( "spGuiGroupedCheckboxes" );

		// HACK: this will make sure we get an array back ($_REQUEST) when no checkboxes are selected
		parent::addChild( new spInput(array(
			"type" => "hidden",
			"name" => $param["name"]."[]",
			"value" => ""
		)));
		
		foreach ( $param["groups"] as $g )
		{
			$fs = new spFieldSet;
			$fs->addChild( new spLegend(array(
				"content" => $g["title"]
			)));
			parent::addChild( $fs );
			
			$items = array();
			$subgroups = explode( ",", $g["subgroups"] );
			foreach ( $subgroups as $s )
			{
				if ( $s=="" )
					 continue;
			
				// the value should represent the group value and subgroup values 
				$items[] = array(
					"value" => $g["value"]."-".$s,
					"content" => $param["lookup_subgroups"][$s]
				);
			}
			
			$this->checkboxes[$g["value"]] = new spCheckBoxList(array(
				"name" => $param["name"]
			));
			$this->checkboxes[$g["value"]]->addItems( $items );
			$fs->addChild( $this->checkboxes[$g["value"]] );
		}
	}
	
	public function setElementDataString( $data )
	{
		parent::setElementDataString( $data );
		//spDump( "setElementDataString", $data );
		
		$ar = explode( ",", $data );
		foreach ( $this->checkboxes as $a )
		{
			$a->selectItems( $ar );
		}
	}
}

?>